﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace NewWords
{
    public partial class ScrollingToolBar : Form
    {
        private ScrollText scrText;
        WordList wordForm;
        public ScrollingToolBar()
        {
            InitializeComponent();
            scrText = new ScrollText(this.textBox1, getData());
            this.textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
            scrText.Start();
            
        }

        void textBox1_LostFocus(object sender, EventArgs e)
        {
            if (!scrText.IsStarted)
            {
                scrText.Start();
            }
        }
        string[] getData()
        {
            List<string> stringList = new List<string>();
            SQLiteDataReader reader = DbHelper.SQLiteHelper.ExecuteReader(CommandType.Text, "select spell, phonetic, explanation, ifnull(sample,\'\') as sample, comment from word", null);
            while (reader.Read())
            {
                StringBuilder builder = new StringBuilder();
                builder.Append((string)reader["spell"]);
                builder.Append(' ', 5);
                builder.Append("phonetic: ");
                builder.Append((string)reader["phonetic"]);
                builder.Append(' ', 5);
                builder.Append("explanation: ");
                builder.Append((string)reader["explanation"]);
                builder.Append(' ', 5);
                builder.Append("sample: ");
                builder.Append((string)reader["sample"]);
                builder.Append(' ', 5);
                stringList.Add(builder.ToString());
            }
            return stringList.ToArray();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.FormBorderStyle = (this.FormBorderStyle == FormBorderStyle.None) ? FormBorderStyle.FixedToolWindow : FormBorderStyle.None;
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            this.scrText.Stop();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (null==wordForm || wordForm.IsDisposed)
            {
                wordForm = new WordList();
            }
            wordForm.Location = this.Location + new Size(0, this.Size.Height);
            if (wordForm.Visible)
            {
                wordForm.Hide();
            }
            else
            {
                wordForm.RefreshData();
                wordForm.Show();
            }
        }
    }
}
